package com.chen.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.chen.feign.order.OrderFeignClient;
import com.chen.model.entity.order.OrderInfo;
import com.chen.model.entity.order.OrderItem;
import com.chen.model.entity.pay.PaymentInfo;
import com.chen.pay.mapper.PaymentInfoMapper;
import com.chen.pay.service.PaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Time;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author cgj
 * @date 5/4/25 9:44 AM
 */
@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {
    @Autowired
    private OrderFeignClient orderFeignClient;
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    /**
     * 保存记账本
     *
     * @param orderNo
     * @return
     */
    @Override
    public PaymentInfo savaPaymentInfo(String orderNo) {
        //判断时候已经存在
        PaymentInfo paymentInfo = paymentInfoMapper.selectPaymentInfoByOrderNo(orderNo);
        if (paymentInfo != null) {
            return paymentInfo;
        }
        OrderInfo orderInfo = orderFeignClient.getOrderInfoByOrderNo(orderNo);
        paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPayType(orderInfo.getPayType());
        //方便进行测试
        paymentInfo.setAmount(new BigDecimal(0.01));
        paymentInfo.setUserId(orderInfo.getUserId());
        //设置交易内容
        List<OrderItem> orderItemList = orderInfo.getOrderItemList();
        StringBuilder sb = new StringBuilder();

        for (OrderItem orderItem : orderItemList) {
            sb.append(orderItem.getSkuName()).append("");
        }
        if (sb.toString().length() > 100) {

            paymentInfo.setContent(sb.toString().substring(0, 100));  //只要100个
        } else {
            paymentInfo.setContent(sb.toString());
        }
        paymentInfo.setPaymentStatus(0);
        //保存记账本
        paymentInfoMapper.save(paymentInfo);
        return paymentInfo;
    }

    /**
     * 根据orderNo查询记账本
     * @param orderNo
     * @return
     */
    @Override
    public PaymentInfo selectPaymentInfoByOrderNo(String orderNo) {
        return paymentInfoMapper.selectPaymentInfoByOrderNo(orderNo);
    }

    /**
     * 更新记账本状态
     *
     * @param paramsMap
     * @param outTradeNo
     */
    @Override
    public void updatePaymentInfo(Map<String, String> paramsMap, String outTradeNo) {
         //判断有没有该过
        PaymentInfo paymentInfo = paymentInfoMapper.selectPaymentInfoByOrderNo(outTradeNo);
        if(paymentInfo.getPaymentStatus().intValue()==1){
            return; //已经修改过了
        }
        //没有修改过开始修改
        //封装数据
        paymentInfo.setPaymentStatus(1);
        paymentInfo.setCallbackContent(JSON.toJSONString(paramsMap));
        paymentInfo.setOutTradeNo(paramsMap.get("trade_no")); //可以不用
        paymentInfo.setCallbackTime(new Date());
        //根据id修改记账本状态
        paymentInfoMapper.updateById(paymentInfo);

        // 修改订单数据
        orderFeignClient.updateOrderByOrderNo(paymentInfo.getPaymentStatus(),outTradeNo);
        //更新库存
    }
}
